class MyLinkedList {

  /** Initialize your data structure here. */
  public class MyLinkedListNode{
      int val;
      MyLinkedListNode next;
      MyLinkedListNode prev;
      public MyLinkedListNode(int val){
          this.val = val;
          this.next = null;
          this.prev = null;
      }
  }

  int len;
  MyLinkedListNode dummyHead;
  MyLinkedListNode tail;
  /** Initialize your data structure here. */
  public MyLinkedList() {
      dummyHead = new MyLinkedListNode(-1);
      tail = dummyHead;
      len = 0;
  }

  /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
  public int get(int index) {
      if (index < 0 || index + 1 > len) return -1;
      MyLinkedListNode node = dummyHead.next;
      for (int i = 0; i < len; i ++) {
          if (i==index) return node.val;
          node = node.next;
      }
      return -1;
  }

  /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
  public void addAtHead(int val) {
      if (len == 0) {
          MyLinkedListNode head = new MyLinkedListNode(val); //链表长度为 0 时，dummyHead 和 tail 指向同一个位置，next 为 null
          dummyHead.next = head;
          head.prev = dummyHead;
          tail = head;
          len++;
      }else {
          MyLinkedListNode tmp = dummyHead.next;
          dummyHead.next = new MyLinkedListNode(val);
          dummyHead.next.prev = dummyHead;
          tmp.prev = dummyHead.next;
          dummyHead.next.next = tmp;
          len++;
      }

  }

  /** Append a node of value val to the last element of the linked list. */
  public void addAtTail(int val) {
      tail.next = new MyLinkedListNode(val);
      tail.next.prev = tail;
      tail = tail.next;
      len++;
  }

  /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
  public void addAtIndex(int index, int val) {
      if (index <= 0) addAtHead(val);
      else if (index == len) addAtTail(val);
      else if (index <= len / 2) {    //如果要插入的 index 距离头部更近
          MyLinkedListNode node = dummyHead;
          for (int i = 0; i < index; i++) {
              node = node.next;
          }
          MyLinkedListNode tmp = node.next;
          node.next = new MyLinkedListNode(val);
          node.next.prev = node;
          node.next.next = tmp;
          tmp.prev = node.next;
          len++;
      }else if(index > len / 2 && index < len) {  //如果要插入的 index 距离尾部更近
          MyLinkedListNode node = tail;
          for (int i = len - 1; i >= index; i--) {
              node = node.prev;
          }
          MyLinkedListNode tmp = node.next;
          node.next = new MyLinkedListNode(val);
          node.next.prev = node;
          node.next.next = tmp;
          tmp.prev = node.next;
          len++;
      }
  }

  /** Delete the index-th node in the linked list, if the index is valid. */
  public void deleteAtIndex(int index) {
      if (index >= 0 && index < len) {
          if (index <= len / 2) {     //如果要删除的 index 距离头部更近
              MyLinkedListNode node = dummyHead;
              for (int i = 0; i < index; i++) {
                  node = node.next;
              }
              node.next = node.next.next;
              if (node.next!=null) node.next.prev = node;
              else tail = node;
              len--;
          }else if(index > len / 2) {     //如果要删除的 index 距离尾部更近
              MyLinkedListNode node = tail;
              for (int i = len - 1; i >= index; i--) {
                  node = node.prev;
              }
              node.next = node.next.next;
              if (node.next!=null) node.next.prev = node;
              else tail = node;
              len--;
          }
      }

  }
}

/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/

